package com.greendragon.persistence.domain; 

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.IndexColumn;

/** 
 * @author wulibin, exceptsky@gmail.com, 2011-9-8 下午11:03:56
 * 
 */
@Entity
@Table(name="product")
public class Product implements Serializable{
	private static final long serialVersionUID = 6315389167832326318L;

	private Long id;
	private String name;													//标题
	private String description;												//描述	
	private Resource coverImg;												//图片
	private Integer soldNum = new Integer(0);								//售出数量
	private Integer commentNum = new Integer(0);							//评论数
	private Double price;													//价格
	private Boolean allowComment = new Boolean(true);						//是否允许评论
	private Boolean allowAnonymousComment = new Boolean(false);				//是否允许匿名评论
	private Date createdAt;													//创建日期
	private Date modifiedAt;												//最后修改日期
	private Boolean block = new Boolean(false);								//是否冻结产品
	//private Collection<ProductComment> productComments;					//产品评论											//评论
	private List<Resource> photos;											//相册
	private ProductCategory category;										//分类
	private User author;													//作者
	
	
	public Product(){}

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Column(name="name", length=50)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Lob
	@Column(name="description")
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@ManyToOne
    @JoinColumn(name="image_id")
	public Resource getCoverImg() {
		return coverImg;
	}

	public void setCoverImg(Resource coverImg) {
		this.coverImg = coverImg;
	}

	@Column(name="getPrice")
	public Double getPrice() {
		return price;
	}

	public void setPrice(Double price) {
		this.price = price;
	}
	
	@Column(name="allowComment")
	public Boolean getAllowComment() {
		return allowComment;
	}

	public void setAllowComment(Boolean allowComment) {
		this.allowComment = allowComment;
	}

	@Column(name="comment_num")
	public Integer getCommentNum() {
		return commentNum;
	}

	public void setCommentNum(Integer commentNum) {
		this.commentNum = commentNum;
	}

	@Column(name="allowAnonymousComment")
	public Boolean getAllowAnonymousComment() {
		return allowAnonymousComment;
	}

	public void setAllowAnonymousComment(Boolean allowAnonymousComment) {
		this.allowAnonymousComment = allowAnonymousComment;
	}

	@Column(name="createdAt")
	@Temporal(TemporalType.TIMESTAMP)
	public Date getCreatedAt() {
		return createdAt;
	}

	public void setCreatedAt(Date createdAt) {
		this.createdAt = createdAt;
	}
	
	@Column(name="modifiedAt")
	@Temporal(TemporalType.TIMESTAMP)
	public Date getModifiedAt() {
		return modifiedAt;
	}

	public void setModifiedAt(Date modifiedAt) {
		this.modifiedAt = modifiedAt;
	}
	
	@Column(name="block")
	public Boolean getBlock() {
		return block;
	}

	public void setBlock(Boolean block) {
		this.block = block;
	}
/*
	@OneToMany(mappedBy="product",cascade=CascadeType.ALL, fetch=FetchType.LAZY)
	public Collection<ProductComment> getProductComments() {
		return productComments;
	}

	public void setProductComments(Collection<ProductComment> comments) {
		this.productComments = comments;
	}
*/
	@OneToMany
	@JoinTable(
        name="productImgs",
        joinColumns = @JoinColumn( name="product_id"),
        inverseJoinColumns = @JoinColumn( name="image_id")
	)
	@IndexColumn(name="position")
	public List<Resource> getPhotos() {
		return photos;
	}

	public void setPhotos(List<Resource> photos) {
		this.photos = photos;
	}

	@ManyToOne
    @JoinColumn(name="category_id", insertable=false, updatable=false, nullable=false)
	public ProductCategory getCategory() {
		return category;
	}

	public void setCategory(ProductCategory category) {
		this.category = category;
	}

	@ManyToOne
	@JoinColumn(name="author_id")
	public User getAuthor() {
		return author;
	}

	public void setAuthor(User author) {
		this.author = author;
	}

	@Column(name="soldNum")
	public Integer getSoldNum() {
		return soldNum;
	}

	public void setSoldNum(Integer soldNum) {
		this.soldNum = soldNum;
	}

}
 